package com.shanling.shanlingcontroller.ui.activity;

import android.Manifest;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.util.Consumer;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.shanling.shanlingcontroller.CustomApplication;
import com.shanling.shanlingcontroller.R;
import com.shanling.shanlingcontroller.base.BaseAppCompatActivity;
import com.shanling.shanlingcontroller.base.BaseAppManager;
import com.shanling.shanlingcontroller.bean.EventCenter;
import com.shanling.shanlingcontroller.ui.fragment.LoginFragment;
import com.shanling.shanlingcontroller.ui.fragment.RegistFragment;
import com.shanling.shanlingcontroller.utils.Md5Util;
import com.shanling.shanlingcontroller.utils.ToastUtils;
import com.shanling.shanlingcontroller.utils.netstatus.NetUtils;




import butterknife.BindView;
import butterknife.OnClick;
import rx.Observer;

public class LoginActivity extends BaseAppCompatActivity {
    @BindView(R.id.tv_login)
    TextView tvLogin;
    @BindView(R.id.tv_register)
    TextView tvRegister;
    @BindView(R.id.login_frame)
    FrameLayout loginFrame;
    private FragmentManager fManager;//管理Fragment界面
    private LoginFragment loginFragment;
    private RegistFragment registFragment;
    private FragmentTransaction transaction;
    @Override
    protected void getBundleExtras(Bundle extras) {

    }
    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_login;
    }
    @Override
    protected void initViewsAndEvents() {
        fManager = getSupportFragmentManager();
        setChoiceItem(0);
    }
    @Override
    protected void initData() {

    }
    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected void onEventComing(EventCenter eventCenter) {

    }


    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return null;
    }

    @OnClick({R.id.tv_login, R.id.tv_register})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_login:
                setChoiceItem(0);
                break;
            case R.id.tv_register:
                setChoiceItem(1);
                break;
        }
    }



    /**
     * Fragment切换
     *
     * @param index
     */
    private void setChoiceItem(int index) {
        transaction = fManager.beginTransaction();
        hideFragments(transaction);
        switch (index) {
            case 0:
                tvLogin.setTextColor(0xffCAB487);
                tvLogin.setTextSize(28);
                tvRegister.setTextSize(20);
                tvRegister.setTextColor(0xffffffff);
                if (loginFragment == null) {
                    loginFragment = new LoginFragment();
                    transaction.add(R.id.login_frame, loginFragment);
                } else {
                    transaction.show(loginFragment);
                }
                break;
            case 1:
                tvRegister.setTextColor(0xffCAB487);
                tvRegister.setTextSize(28);
                tvLogin.setTextSize(20);
                tvLogin.setTextColor(0xffffffff);
                    if (registFragment == null) {
                        registFragment = new RegistFragment();
                        transaction.add(R.id.login_frame, registFragment);
                    } else {
                        transaction.show(registFragment);
                    }
                break;
        }
        transaction.commit();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        transaction.remove(loginFragment);
        transaction.remove(registFragment);

    }

    /**
     * 隐藏片段
     *
     * @param transaction
     */
    private void hideFragments(FragmentTransaction transaction) {
        if (loginFragment != null) {
            transaction.hide(loginFragment);
        }
        if(registFragment!=null){
            transaction.hide(registFragment);
        }
    }

    private long exitTime;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                ToastUtils.showToast(this,R.string.press_again_to_exit);
                exitTime = System.currentTimeMillis();
            } else {
                BaseAppManager.getInstance().exit(false);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }




}
